package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLDeviceGroupDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceBindGroupBean;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.GroupAddEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.ui.adapter.DeviceGroupListAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DeviceGroupListActivity extends AppActivity{
    private RecyclerView list;
    private DeviceGroupListAdapter adapter;
    private TextView addOtherTextview;
    public int viewType;//1为设备组管理，2为设备组
    private String deviceSerialNo;
    private LinearLayout ll_Noequipment;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, DeviceGroupListActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_group_list;
    }

    @Override
    protected void initView() {
        String title = getString(R.string.devicegroup_title);
        if(!TextUtils.isEmpty(title)){
            setTitle(title);
        }
        getTitleBar().setRightTitle("");
        EventBus.getDefault().register(this);
        viewType = getIntent().getIntExtra("viewType",1);
        addOtherTextview = findViewById(R.id.btn_addOtherGroup);
        addOtherTextview.setVisibility(viewType == 1 ? View.GONE : View.VISIBLE);
        setOnClickListener(R.id.btn_addOtherGroup);
        ll_Noequipment = findViewById(R.id.ll_Noequipment);
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new DeviceGroupListAdapter(getContext(), false, true);
        adapter.adapterType = viewType;
        adapter.setOnChildClickListener(R.id.add_group, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                gotoGroupAddView(position);
            }
        });
        adapter.setOnChildClickListener(R.id.btn_add_group, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                gotoGroupAddView(position);
            }
        });
        list.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        if(viewType == 1){
            List<ControllerBean> controllerBeans = LLControllerDBManager.getInstance(getContext()).getAllControllerGroup();
            if(controllerBeans.size() == 0){
                ll_Noequipment.setVisibility(View.VISIBLE);
                list.setVisibility(View.GONE);
            }else{
                ll_Noequipment.setVisibility(View.GONE);
                list.setVisibility(View.VISIBLE);
                adapter.setData(controllerBeans);
            }
        }else {
            deviceSerialNo = getIntent().getStringExtra("deviceSerialNo");
            String controllerSerialNo = getIntent().getStringExtra("controllerSerialNo");
            ControllerBean controllerBean = LLControllerDBManager.getInstance(getContext()).getControllerBySerialNo(controllerSerialNo);
            List<DeviceBindGroupBean> deviceBindGroupBeans = LLDeviceGroupDBManager.getInstance(getContext()).getDeviceGroupsByDeviceSerialNo(deviceSerialNo);
            List<String> deviceSerialNos = new ArrayList<>();
            for(int i = 0;i < deviceBindGroupBeans.size();i++){
                DeviceBindGroupBean groupBean = deviceBindGroupBeans.get(i);
                deviceSerialNos.add(groupBean.getDeviceGroupSerialNo());
            }
            List<DeviceBean> deviceBeans = LLDeviceDBManager.getInstance(getContext()).getDevicesByDeviceSerialNos(deviceSerialNos);
            controllerBean.setDeviceItems(deviceBeans);
            List<ControllerBean> controllerBeans = new ArrayList<>();
            controllerBeans.add(controllerBean);
            adapter.setData(controllerBeans);
        }
    }

    public void gotoGroupSettingVIew(DeviceBean deviceBean){
        DeviceSettingActivity.start(getContext(),deviceBean.getDeviceSerialNo(),true);
    }

    public void gotoGroupAddView(int position){
        ControllerBean controllerBean = adapter.getData().get(position);
        GroupDeviceListActivity.start(getContext(),"",controllerBean.getControllerSerialNo(),viewType);
    }

    public void gotoGroupAddView(int controllerPosition,int position){
        ControllerBean controllerBean = adapter.getData().get(controllerPosition);
        DeviceBean deviceBean = controllerBean.getDeviceItems().get(position);
        GroupDeviceListActivity.start(getContext(),deviceBean.getDeviceSerialNo(),controllerBean.getControllerSerialNo(),deviceSerialNo,deviceBean.getName(),viewType);
    }

    public void gotoEnrollmentView(){
        ControllerBean controllerBean = adapter.getData().get(0);
        List<DeviceBean> deviceBeans = controllerBean.getDeviceItems();
        List<String> serialNos = new ArrayList<>();
        for(int i = 0;i < deviceBeans.size();i++){
            DeviceBean deviceBean = deviceBeans.get(i);
            serialNos.add(deviceBean.getDeviceSerialNo());
        }
        Intent intent = new Intent(getContext(), GroupEnrollmentActivity.class);
        intent.putExtra("deviceSerialNo", deviceSerialNo);
        intent.putExtra("controllerSerialNo", controllerBean.getControllerSerialNo());
        intent.putExtra("containDevice", (Serializable) serialNos);
        getContext().startActivity(intent);
    }

    @Override
    public void onRightClick(View view) {
        if(adapter.getSelectedData() == null || adapter.getSelectedData().size() == 0){
            toast(R.string.please_select_room_hint);
            return;
        }
        Intent intent = new Intent();
//        intent.putExtra("serialNo", adapter.getSelectedData().get(0).getRoomSerialNo());
//        intent.putExtra("roomSerialNo", adapter.getSelectedData().get(0).getRoomSerialNo());
//        intent.putExtra("roomName", adapter.getSelectedData().get(0).getRoomName());
//        intent.putExtra("floorSerialNo", adapter.getSelectedData().get(0).getFloorSerialNo());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected  void onDestroy(){
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshDeviceListEvent(RefreshDeviceListEvent event) {
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGroupAddEvent(GroupAddEvent event) {
        initData();
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_addOtherGroup:
                gotoEnrollmentView();
                break;
        }
    }
}